package com.woniuxy.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.comon.RedisConst;
import com.woniuxy.portal.common.enumeration.Assert;
import com.woniuxy.portal.common.enumeration.BusinessCode;
import com.woniuxy.portal.common.enumeration.UserStatus;
import com.woniuxy.portal.model.dto.LoginRequest;
import com.woniuxy.portal.model.dto.UserVo;
import com.woniuxy.portal.model.entity.User;
import com.woniuxy.portal.service.UserService;
import com.woniuxy.portal.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author liboyang
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-10-18 14:25:01
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseEntity<?> login(LoginRequest param) {
        //获取验证码的key
        String key = param.getKey();
        //得到Redis中验证码
        String code = stringRedisTemplate.opsForValue().get(key);
        //判断验证码是否过期
        Assert.error(code == null, BusinessCode.INVALID_VERIFICATION);
        //判断验证码是否正确
        Assert.error(!code.equalsIgnoreCase(param.getCode()), BusinessCode.VERIFICATION_ERROR);

        //删除Redis中的验证码
        stringRedisTemplate.delete(key);

        //判断用户名是否存在
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getAccount, param.getAccount());
        User user = userMapper.selectOne(wrapper);
        Assert.error(user == null, BusinessCode.USER_NOT_EXIST);

        //判断密码是否正确
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(param.getPassword(), user.getPassword());
        Assert.error(!matches, BusinessCode.PASSWORD_ERROR);

        //判断是否账户可用
        Assert.error(user.getState() != UserStatus.ACTIVE.getKey(), BusinessCode.USER_FROZEN_ERROR);

        //登录成功
        return ResponseEntity.ok(user);
    }

    @Override
    @Cacheable(cacheNames = RedisConst.USER_DETAIL, keyGenerator = "keyGenerator")
    public UserVo findById(Long id) {
        //调用数据层查询
        User user = userMapper.selectById(id);
        //复制属性
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }


    //通过手机号查询用户
    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile);
        return userMapper.selectOne(wrapper);
    }

    //判断邮箱是否存在
    @Override
    public boolean isEmailExist(String email) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getEmail, email);
        return userMapper.selectOne(wrapper) != null;
    }

    //判断账户名是否存在
    @Override
    public boolean isAccountExist(String account) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getAccount, account);
        return userMapper.selectOne(wrapper)!= null;
    }

    //判断电话是否存在
    @Override
    public boolean isMobileExist(String mobile) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile);
        return userMapper.selectOne(wrapper)!= null;
    }

    //注册成功，返回新的用户对象
    @Override
    public User register(User user) {
        //对密码进行加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //设置用户状态
        user.setState(UserStatus.ACTIVE.getKey());
        //写入数据库，会自动更新user的id值
        userMapper.insert(user);
        return user;
    }

}




